import {
    getMenuByRouter,
    setTagNavListInLocalstorage,
    getTagNavListFromLocalstorage,
    getHomeRoute,
    getNextRoute,
    routeHasExist,
    routeEqual,
    getRouteTitleHandled
} from '@/utils/index'
// import localStorage from '@/utils/localStorage'
import router from '@/router'
import config from '@/config'
// const local = localStorage.local

const { homeName } = config
const state = {
    breadCrumbList: [],
    tagNavList: [],
    homeRoute: {}
}
const closePage = (state, route) => {
    const nextRoute = getNextRoute(state.tagNavList, route)
    state.tagNavList = state.tagNavList.filter(item => {
        return !routeEqual(item, route)
    })
    // console.log(nextRoute)
    router.push(nextRoute)
}
const mutations = {
    setHomeRoute (state, routes) {
        state.homeRoute = getHomeRoute(routes, homeName)
    },
    setTagNavList (state, list) {
        state.tagNavList = list
        setTagNavListInLocalstorage([...list])
    },
    closeTag (state, route) {
        let tag = state.tagNavList.filter(item => routeEqual(item, route))
        route = tag[0] ? tag[0] : null
        if (!route) return
        closePage(state, route)
    },
    addTag (state, { route, type = 'unshift' }) {
        let router = getRouteTitleHandled(route)
        if (!routeHasExist(state.tagNavList, router)) {
            if (type === 'push') state.tagNavList.push(router)
            else {
                if (router.name === homeName) state.tagNavList.unshift(router)
                else state.tagNavList.splice(1, 0, router)
            }
        } else {
            state.tagNavList.forEach((el, index) => {
                if (el.name === route.name) {
                    state.tagNavList.splice(index, 1, router)
                }
            })
        }
        setTagNavListInLocalstorage([...state.tagNavList])
    }
}

const actions = {
    setTagNavList ({ state, rootState, commit }, list) {
        let tagList = []
        if (list) {
            tagList = [...list]
        } else tagList = getTagNavListFromLocalstorage() || []
        if (tagList[0] && tagList[0].name !== homeName) tagList.shift()
        let homeTagIndex = tagList.findIndex(item => item.name === homeName)
        if (homeTagIndex > 0) {
            let homeTag = tagList.splice(homeTagIndex, 1)[0]
            tagList.unshift(homeTag)
        } else if (!tagList.length && homeTagIndex < 0) {
            tagList = [state.homeRoute]
        }
        // 判断是否有该页签的权限
        let roles = rootState.user.roles || []
        let tags = []
        tagList.map(el => {
            if ((el.meta && el.meta.roles && roles.includes(el.meta.roles[0])) || el.name === 'home') {
                tags.push(el)
            }
        })
        tagList = tags
        commit('setTagNavList', tagList)
    },
    closeTag ({ state, commit }, route) {
        commit('closeTag', route)
    }
}
export default {
    state,
    getters: {
        menuList: (state, getters, rootState) => {
            // debugger
            return getMenuByRouter(rootState.user.menuList, rootState.user.roles) || []
        },
        tagNavList: (state, getters, rootState) => state.tagNavList
    },
    mutations,
    actions
}
